package com.cxd.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.cxd.springcloud.entities.Payment;
import com.cxd.springcloud.myhand.CustomBlockHandler;
import com.cxd.springcloud.vo.CommonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {

    @GetMapping("/bySource")
    @SentinelResource(value = "bySource",blockHandler = "block_handler")
    public CommonResult<Payment> bySource() {
        return new CommonResult<Payment>("666", "通过资源名称进行限流",new Payment(777L, "7758258"));
    }


    public CommonResult<Payment> block_handler(BlockException exception) {
        return new CommonResult<Payment>("555", "通过资源名称进行限流", null);
    }

    @GetMapping("/rateLimit/byUrl")
    public CommonResult<Payment> byUrl() {
        return new CommonResult<Payment>("666", "通过URL进行限流");
    }

    @GetMapping("/rateLimit/customBlockHandler")
    @SentinelResource(value = "customBlockHandler",blockHandlerClass = CustomBlockHandler.class,blockHandler = "blockHandlerException2")
    public CommonResult customBlockHandler() {
        return new CommonResult("777", "通过自定义类 customBlockHandler进行限流");
    }
}
